import {
	api_getContentlist,
	api_getSites
} from '../../api/api.transit.js'
export default {
	namespaced: true,
	state: {
		beginSite:"",//开始站点
		beginSiteList:[],//查询开始站点列表
		endSite:"",//结束站点
		endSiteList:[],//查询结束站点列表
		transferScheme: [],//查询换乘方案
		schemeDescription:[]//方案详情
	},
	getters: {

	},
	mutations: {
		SWITCH_INFO(state){
			let beginSite=state.beginSite;
			let endSite=state.endSite;
			state.beginSite=endSite;
			state.endSite=beginSite;
		},
		SET_TRANFER_SCHEME(state, data) {
			state.transferScheme = data;
		},
		SET_BEGIN_SITE_LIST(state,data){
			state.beginSiteList=data
		},
		SET_END_SITE_LIST(state,data){
			state.endSiteList=data
		},
		SET_BEGIN_SITE(state,data){
			state.beginSite=data
		},
		SET_END_SITE(state,data){
			state.endSite=data
		},
		SET_SCHEME_DESCRIPTION(state,data){
			state.schemeDescription=data
		}
	},
	actions: {
		GET_TRANFER_SCHEME({state,commit,rootState}, data) {
			return new Promise((resolve, reject) => {
				const data = {
					"cityName":rootState.CityName,
					"beginSite": state.beginSite,
					"endSite": state.endSite,
					"isLineId": "1",
					"isSiteId": "1"
				}
				api_getContentlist(data)
					.then((res) => {
						if (res.showapi_res_body) {
							const body =  res.showapi_res_body.contentlist[0].conArray
							commit('SET_TRANFER_SCHEME', body)
							resolve(body)
						} else {
							commit('SET_TRANFER_SCHEME', [])
							reject()
						}
					})
					.catch(() => {
						reject()
					})
			})
		},
		GET_BEGIN_SITE_LIST({commit,rootState}, data){
			return new Promise((resolve, reject) => {
				data.cityName=rootState.CityName
				api_getSites(data)
					.then((res) => {
						if(res.showapi_res_body.soulist){
							const body=res.showapi_res_body.soulist
							commit('SET_BEGIN_SITE_LIST', body)
							resolve(body)
						}else{
							commit('SET_BEGIN_SITE_LIST', [])
							reject()
						}
					})
					.catch(() => {
						reject()
					})
			})
		},
		GET_END_SITE_LIST({rootState,commit}, data){
			return new Promise((resolve, reject) => {
				data.cityName=rootState.CityName
				api_getSites(data)
					.then((res) => {
						if(res.showapi_res_body.soulist){
							const body=res.showapi_res_body.soulist
							commit('SET_END_SITE_LIST', body)
							resolve(body)
						}else{
							commit('SET_END_SITE_LIST', [])
							reject()
						}
					})
					.catch(() => {
						store.commit('SET_END_SITE_LIST', [])
						reject()
					})
			})
		},
		PASS_LINE({commit},id){
            commit('lineDetails/SET_LINE_ID', id, {root: true}) 
        },
	}
}
